﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;

namespace Client
{
    public partial class FormLogin : Form
    {
        const int LOGIN = 0;
        const int REG = 1;
        const int EXIT = 2;
        const int LOGINSUCCESSFULL = 3;

        private TcpClient tcpClient;
        private bool flagLogedin = false;

        public FormLogin(TcpClient tcpClient)
        {
            InitializeComponent();
            this.tcpClient = tcpClient;
        }

        public bool getLoginStatus()
        {
            return flagLogedin;
        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            if(textBoxUserName.Text!=null&&textBoxPass.Text!=null)
            {
                byte[] buffer = BitConverter.GetBytes(LOGIN);
                NetworkStream ns = tcpClient.GetStream();
                ns.Write(buffer, 0, 4);//send opcode

                byte[] lenBuffer = BitConverter.GetBytes(textBoxUserName.Text.Length);
                ns.Write(lenBuffer, 0, 4);//send length of username
                buffer = Encoding.ASCII.GetBytes(textBoxUserName.Text);
                ns.Write(buffer, 0, buffer.Length);//send username

                lenBuffer = BitConverter.GetBytes(textBoxPass.Text.Length);
                ns.Write(lenBuffer, 0, 4);//send length of pass
                buffer = Encoding.ASCII.GetBytes(textBoxPass.Text);
                ns.Write(buffer, 0, buffer.Length);//send pass
                byte[] opCodeBuffer = new byte[4];

                
            }
            else
            {

            }
           

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textBoxUserName_TextChanged(object sender, EventArgs e)
        {
            if (textBoxPass.Text!=""&&textBoxUserName.Text!="")
            {
                buttonLogin.Enabled = true;
            } 
            else
            {
                buttonLogin.Enabled = false;
            }
        }

        private void textBoxPass_TextChanged(object sender, EventArgs e)
        {
            if (textBoxPass.Text != "" && textBoxUserName.Text != "")
            {
                buttonLogin.Enabled = true;
            }
            else
            {
                buttonLogin.Enabled = false;
            }
        }
    }
}
